package com.easytake.emotioncat.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.http.HttpStatus;

import java.util.Optional;

@Data
@Schema(title = "响应对象")
public class Response<J> {
    @Schema(title = "响应码：200-成功")
    private Integer code;

    @Schema(title = "提示信息")
    private String msg;

    @Schema(title = "结果对象")
    private J data;

    public static <T> Response<T> success(String msg, T data) {
        Response<T> response = success(data);
        response.msg = Optional.ofNullable(msg).orElse("成功");
        return response;
    }

    public static <T> Response<T> success(T data) {
        Response<T> response = success();
        response.setData(data);
        return response;
    }

    public static <T> Response<T> success() {
        Response<T> response = new Response<>();
        response.code = HttpStatus.OK.value();
        response.msg = ("成功");
        return response;
    }

    public static <T> Response<T> fail(Integer code, String msg) {
        Response<T> response = new Response<>();
        response.code = code;
        response.msg = Optional.ofNullable(msg).orElse("失败");
        return response;
    }

    public static <T> Response<T> fail(String msg) {
        Response<T> response = new Response<>();
        response.code = 500;
        response.msg = Optional.ofNullable(msg).orElse("失败");
        return response;
    }
}
